﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using CommonLibrary.DB;
using CommonLibrary.DataModel.Basic;
using System.Data;

namespace ServiceApplication.Dao.Basic
{
    public class MsgmxDao
    {
        public int AddEntity(MsgmxEntity msgmxEntity)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@msgmx_id", msgmxEntity.msgmx_id));
            parameterList.Add(new SqlParameter("@msg_id", msgmxEntity.msg_id));
            parameterList.Add(new SqlParameter("@receiver_id", msgmxEntity.receiver_id));
            parameterList.Add(new SqlParameter("@msgmx_isread", msgmxEntity.msgmx_isread));


            string sql = "insert into jc_msgmx (msg_id,receiver_id,msgmx_isread) values " +
                " (@msg_id,@receiver_id,@msgmx_isread); " +
                " select @@identity";
            string id = DBExecuteUtil.getUniqueResult(sql, parameterList.ToArray());
            return int.Parse(id);
        }

        public void DleteEntity(int msgmx_id)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@msgmx_id", msgmx_id));
            string sql = "delete from jc_msgmx where msgmx_id=@msgmx_id ";
            DBExecuteUtil.executeSqlWithPar(sql, parameterList.ToArray());
        }

        public void UpdateEntity(MsgmxEntity msgmxEntity)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@msgmx_id", msgmxEntity.msgmx_id));
            parameterList.Add(new SqlParameter("@msg_id", msgmxEntity.msg_id));
            parameterList.Add(new SqlParameter("@receiver_id", msgmxEntity.receiver_id));
            parameterList.Add(new SqlParameter("@msgmx_isread", msgmxEntity.msgmx_isread));

            string sql = "update jc_msgmx set msg_id=@msg_id,receiver_id=@receiver_id,msgmx_isread=@msgmx_isread where msgmx_id=@msgmx_id ";

            DBExecuteUtil.executeSqlWithPar(sql, parameterList.ToArray());
        }

        public MsgmxEntity QueryEntity(int msgmx_id)
        {
            MsgmxEntity msgmxEntity = null;
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@msgmx_id", msgmx_id));
            string sql = "select msgmx_id,msg_id,receiver_id,msgmx_isread  from jc_msgmx where msgmx_id=@msgmx_id";

            var result = DBExecuteUtil.querySqlArray(sql, parameterList.ToArray());
            if (result == null) return null;

            msgmxEntity = new MsgmxEntity();
            msgmxEntity.msgmx_id = int.Parse(result[0][0]);
            msgmxEntity.msg_id = int.Parse(result[0][1]);
            msgmxEntity.receiver_id = int.Parse(result[0][2]);
            msgmxEntity.msgmx_isread = int.Parse(result[0][3]);
            return msgmxEntity;
        }

        public MsgmxEntity QueryEntity(int msg_id, int receiver_id)
        {
            MsgmxEntity msgmxEntity = null;
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@msg_id", msg_id));
            parameterList.Add(new SqlParameter("@receiver_id", receiver_id));
            string sql = "select msgmx_id,msg_id,receiver_id,msgmx_isread  from jc_msgmx where msg_id=@msg_id and receiver_id=@receiver_id ";

            var result = DBExecuteUtil.querySqlArray(sql, parameterList.ToArray());
            if (result == null) return null;

            msgmxEntity = new MsgmxEntity();
            msgmxEntity.msgmx_id = int.Parse(result[0][0]);
            msgmxEntity.msg_id = int.Parse(result[0][1]);
            msgmxEntity.receiver_id = int.Parse(result[0][2]);
            msgmxEntity.msgmx_isread = int.Parse(result[0][3]);
            return msgmxEntity;
        }

        public DataTable QueryByReceiverId(int receiver_id)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@receiver_id", receiver_id));
            string sql = "select msgmx_id 消息明细编码,msg_id 消息编码,user_name 接收人,receiver_id 接收人编码,msgmx_isread 已读标志  from jc_msgmx msgmx,jc_user u where msgmx.receiver_id=u.user_id and receiver_id=@receiver_id ";

            var dt = DBExecuteUtil.querySqlTable(sql, parameterList.ToArray());
            return dt;
        }

        public DataTable QueryByMsgId(int msg_id)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@msg_id", msg_id));
            string sql = "select msgmx_id 消息明细编码,msg_id 消息编码,user_name 接收人,receiver_id 接收人编码,msgmx_isread 已读标志  from jc_msgmx msgmx,jc_user u where msgmx.receiver_id=u.user_id  and msg_id=@msg_id ";

            var dt = DBExecuteUtil.querySqlTable(sql, parameterList.ToArray());
            return dt;
        }

        public void UpdateReadFlag(int msg_id, int receiver_id, int msgmx_isread)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@msg_id", msg_id));
            parameterList.Add(new SqlParameter("@msgmx_isread", msgmx_isread));
            parameterList.Add(new SqlParameter("@receiver_id", receiver_id));
            string sql = "update jc_msgmx set msgmx_isread=@msgmx_isread where msg_id=@msg_id and receiver_id=@receiver_id  ";

            DBExecuteUtil.executeSqlWithPar(sql, parameterList.ToArray());

        }
    }
}
